﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace GPSTrackingDBCustom.Log
{
    public class WindowsServiceLog
    {
        enum Category
        {
            Information = 0,
            Warning = 1,
            Error  = 2
        }

        const string EventLogName = "GPSTrackingSoftware";

        private static void CreateEventLog()
        {
            System.Diagnostics.EventLog.CreateEventSource(EventLogName, "WindowsServiceLog");            
        }

        private static string GetCallingMethod(StackFrame frame)
        {            
            var method = frame.GetMethod();
            return method.DeclaringType.FullName + "." + method.Name;
        }
        
        /// <summary>
        /// Logs an informational message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Info(string message, string gpsModel)
        {            
            //if (!System.Diagnostics.EventLog.SourceExists(EventLogName)) CreateEventLog();

            //System.Diagnostics.EventLog EventLog = new System.Diagnostics.EventLog();
            //EventLog.Source = EventLogName;
            //EventLog.WriteEntry("Model: "+gpsModel+" - "+message, EventLogEntryType.Information);
            
            GPSTrackingDBV2.Entities.WindowsServicesLog LogEntity = new GPSTrackingDBV2.Entities.WindowsServicesLog();            
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Information.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));            
            LogEntity.Message = message;
            LogEntity.GpsModel = gpsModel;

            GPSTrackingDBV2.Data.DataRepository.WindowsServicesLogProvider.Insert(LogEntity);

        }

        /// <summary>
        /// Logs a warning message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Warning(string message, string gpsModel)
        {
            //if (!System.Diagnostics.EventLog.SourceExists(EventLogName)) CreateEventLog();

            //System.Diagnostics.EventLog EventLog = new System.Diagnostics.EventLog();
            //EventLog.Source = EventLogName;
            //EventLog.WriteEntry("Model: " + gpsModel + " - " + message, EventLogEntryType.Warning);
            
            GPSTrackingDBV2.Entities.WindowsServicesLog LogEntity = new GPSTrackingDBV2.Entities.WindowsServicesLog();            
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Warning.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;
            LogEntity.GpsModel = gpsModel;

            GPSTrackingDBV2.Data.DataRepository.WindowsServicesLogProvider.Insert(LogEntity);         
        }

        /// <summary>
        /// Logs an error message
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Error(string message, string gpsModel)
        {
            //if (!System.Diagnostics.EventLog.SourceExists(EventLogName)) CreateEventLog();

            //System.Diagnostics.EventLog EventLog = new System.Diagnostics.EventLog();
            //EventLog.Source = EventLogName;
            //EventLog.WriteEntry("Model: " + gpsModel + " - " + message, EventLogEntryType.Error);

            GPSTrackingDBV2.Entities.WindowsServicesLog LogEntity = new GPSTrackingDBV2.Entities.WindowsServicesLog();            
            LogEntity.DateTime = DateTime.Now;
            LogEntity.Category = Category.Error.ToString();
            LogEntity.Method = GetCallingMethod(new StackFrame(1));
            LogEntity.Message = message;
            LogEntity.GpsModel = gpsModel;

            GPSTrackingDBV2.Data.DataRepository.WindowsServicesLogProvider.Insert(LogEntity); 
        }
    }
}
